from lexer import Lexer

class GrammarLexer(Lexer):
    def __init__(self, string):
        self.token = ""
        
        self.tokenChars = "()+*?%|"
        
        Lexer.__init__(self, string)
        
    def endLiteral(self):
        if self.token:
            if self.token.endswith(":"):
                self.addToken(self.token[:-1],"ruleName")
            else:
                self.addToken(self.token)
        self.token = ""
        
    def lex_root(self, char):
        if char.isspace():
            self.endLiteral()
            
        elif char == "\"":
            self.endLiteral()
            self.startState("string")
            
        elif char == "#":
            self.endLiteral()
            self.startState("comment")
            
        elif char in self.tokenChars:
            self.endLiteral()
            self.addToken(char,char)
            
        else:
            self.token += char
            
    def lex_comment(self, char):
        if char == "\n":
            self.endState()
            
    def lex_string(self, char):
        if char == "\"":
            self.addToken(self.token,"string")
            self.token = ""
            self.endState()
        else:
            self.token += char
